package com.bocai.freeman.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.bocai.freeman.base.BaseView;
import com.bocai.freeman.model.UserModel;
import com.bocai.freeman.model.bean.BaseBean;
import com.bocai.freeman.model.bean.UserBean;
import com.bocai.freeman.utils.StringUtil;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import cn.bocweb.net.BocResponse;
import rx.Observer;

/**
 * Created by win7 on 2016/5/18.
 */
public class UserPresenter {
    private BaseView mBaseView;
    private UserModel mUserModel;

    public UserPresenter(BaseView baseView, RxAppCompatActivity activity) {
        mUserModel = new UserModel(activity);
        this.mBaseView = baseView;
    }

    public void obtainCode(String tel, String type) {
        if (!StringUtil.isMobileNO(tel)) {
            mBaseView.showToast("手机号不正确");
        } else {
            mBaseView.showLoading();
            mUserModel.obtainCode(tel, type, new Observer<BaseBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    Log.e("tag",e.getLocalizedMessage());
                    mBaseView.showToast("网络错误");
                    mBaseView.hideLoading();
                }

                @Override
                public void onNext(BaseBean bean) {
                    if (bean.getReturnNo().equals("0000")) {
                        mBaseView.setData(bean);
                    } else {
                        mBaseView.showToast(bean.getReturnInfo());
                    }
                    mBaseView.hideLoading();
                }
            });
        }
    }

    public void register(String tel, String pwd, String code,boolean isAgree) {
        if (!StringUtil.isMobileNO(tel)) {
            mBaseView.showToast("手机号不正确");
        } else if (TextUtils.isEmpty(code)) {
            mBaseView.showToast("验证码不能为空");
        } else if (TextUtils.isEmpty(pwd)) {
            mBaseView.showToast("密码不能为空");
        } else if(!isAgree){
            mBaseView.showToast("请同意协议");
        }else {
            mBaseView.showLoading();
            mUserModel.register(tel, pwd, code, new Observer<BaseBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    mBaseView.showToast("网络错误");
                    mBaseView.hideLoading();
                }

                @Override
                public void onNext(BaseBean bean) {
                    if (bean.getReturnNo().equals("0000")) {
                        mBaseView.setData("注册成功");
                    } else {
                        mBaseView.showToast(bean.getReturnInfo());
                    }
                    mBaseView.hideLoading();
                }
            });
        }
    }

    public void login(String tel, String password) {
        if (!StringUtil.isMobileNO(tel)) {
            mBaseView.showToast("手机号不正确");
        } else if (TextUtils.isEmpty(password)) {
            mBaseView.showToast("密码不能为空");
        } else {
            mBaseView.showLoading();
            mUserModel.login(tel, password, new Observer<BaseBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    mBaseView.showToast("网络错误");
                    mBaseView.hideLoading();
                }

                @Override
                public void onNext(BaseBean bean) {
                    if (bean.getReturnNo().equals("0000")) {
                        UserBean userBean = BocResponse.getInstance().getContent(bean.getContent(), UserBean.class);
                        mBaseView.setData(userBean);
                    } else {
                        mBaseView.showToast(bean.getReturnInfo());
                    }
                    mBaseView.hideLoading();
                }
            });
        }
    }

    public void forgetPwd(String tel, String password, String code) {
        if (!StringUtil.isMobileNO(tel)) {
            mBaseView.showToast("手机号不正确");
        } else if (TextUtils.isEmpty(code)) {
            mBaseView.showToast("验证码不能为空");
        } else if (TextUtils.isEmpty(password)) {
            mBaseView.showToast("密码不能为空");
        } else {
            mBaseView.showLoading();
            mUserModel.forgetPwd(tel, password, code, new Observer<BaseBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    mBaseView.showToast("网络错误");
                    mBaseView.hideLoading();
                }

                @Override
                public void onNext(BaseBean bean) {
                    if (bean.getReturnNo().equals("0000")) {
                        mBaseView.setData("找回成功");
                    } else {
                        mBaseView.showToast(bean.getReturnInfo());
                    }
                    mBaseView.hideLoading();
                }
            });
        }
    }

}
